Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: fix panic caused by outdated feedback #7128

Merged
merged 12 commits into from
Jul 25, 2018

Conversation

alivxxx
Copy link
Contributor

@alivxxx alivxxx commented Jul 23, 2018

What have you changed? (mandatory)

When feedback is outdated, for example, the table has been dropped, it should not cause panic. This PR fixes it by first checking that we do have the corresponding stats.

What is the type of the changes? (mandatory)

  • Bug fix (non-breaking change which fixes an issue)

How has this PR been tested? (mandatory)

Unit test.

Does this PR affect documentation (docs/docs-cn) update? (mandatory)

No.

Does this PR affect tidb-ansible update? (mandatory)

No.

Does this PR need to be added to the release notes? (mandatory)

No.

PTAL @coocood @zz-jason @winoros

@shenli
Copy link
Member

shenli commented Jul 23, 2018

Do we need to cherry-pick this to the release-2.0?

@alivxxx
Copy link
Contributor Author

alivxxx commented Jul 23, 2018

@shenli Yes, we need to.

if err != nil {
return errors.Trace(err)
}
// Update the NDV of primary key column.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not check "hist != nil" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We clear the outdated feedback in dumpStatsUpdateToKV, if check the hist here, we won't call that function, so these feedbacks will remain in the storage forever.

}
h.mu.Lock()
h.mu.ctx.GetSessionVars().BatchDelete = true
sql := fmt.Sprintf("delete from mysql.stats_feedback where table_id = %d and hist_id = %d and is_index = %d", tableID, hist.ID, isIndex)
sql := fmt.Sprintf("delete from mysql.stats_feedback where table_id = %d and hist_id = %d and is_index = %d", tableID, histID, isIndex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to disable batch-delete after running the SQL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

hist.NDV = int64(hist.totalRowCount())
col = nil
}
err = h.dumpStatsUpdateToKV(tableID, int(isIndex), histID, q, hist, cms)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The histID is always -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be initialized when we meet a new group, as indicated here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we group feedbacks in the first loop, then dump the feedbacks in the second loop, we can avoid the case that histID is -1.

@coocood
Copy link
Member

coocood commented Jul 24, 2018

LGTM

@coocood
Copy link
Member

coocood commented Jul 24, 2018

/run-all-tests

coocood
coocood previously approved these changes Jul 24, 2018

q := &QueryFeedback{}
for _, rows := range groupedRows {
tableID, histID, isIndex = rows[0].GetInt64(0), rows[0].GetInt64(1), rows[0].GetInt64(2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we extract the code in the loop in a function, then we can defer deleteOutdatedFeedback

coocood
coocood previously approved these changes Jul 25, 2018
@coocood coocood added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 25, 2018
hist = UpdateHistogram(hist, q)
err := h.SaveStatsToStorage(tableID, -1, int(isIndex), hist, cms, 0)
if err != nil {
metrics.UpdateStatsCounter.WithLabelValues(metrics.LblError).Inc()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use metrics.RetLabel(err).

return nil
}

func (h *Handle) handleSingleHistogramUpdate(is infoschema.InfoSchema, rows []types.Row) (err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comment about:

  1. all the rows with the same "(tableId, histId, isIndex)" values from the feedback are gathered in rows
  2. this functions will update the Histogram and CMS using these feedbacks.

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason
Copy link
Member

/run-all-tests

@zz-jason
Copy link
Member

@coocood @shenli PTAL

Copy link
Member

@shenli shenli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alivxxx alivxxx merged commit 9a1a95d into pingcap:master Jul 25, 2018
@alivxxx alivxxx deleted the feedback-panic branch July 25, 2018 10:30
alivxxx added a commit to alivxxx/tidb that referenced this pull request Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/statistics status/LGT1 Indicates that a PR has LGTM 1. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants